package com.dieusoft.box.yardrate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dieusoft.box.info.entity.JzxDefaultCostDetails;
import com.dieusoft.box.info.mapper.JzxDefaultCostDetailsMapper;
import com.dieusoft.box.yardrate.entity.YardInfo;
import com.dieusoft.box.yardrate.mapper.YardInfoMapper;
import com.dieusoft.box.yardrate.service.IYardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 堆场费率信息表
 * @Author: dongw
 * @Date: 2021-01-29
 * @Version: V1.0
 */
@Service
public class YardInfoServiceImpl extends ServiceImpl<YardInfoMapper, YardInfo> implements IYardInfoService {

    @Autowired
    private YardInfoMapper yardInfoMapper;
    @Autowired
    private JzxDefaultCostDetailsMapper jzxDefaultCostDetailsMapper;

    @Override
    @Transactional
    public void saveMain(YardInfo yardInfo, List<JzxDefaultCostDetails> jzxDefaultCostDetailsList) {
        yardInfoMapper.insert(yardInfo);
        if (jzxDefaultCostDetailsList != null && jzxDefaultCostDetailsList.size() > 0) {
            for (JzxDefaultCostDetails entity : jzxDefaultCostDetailsList) {
                //外键设置
                entity.setCostId(yardInfo.getId());
                jzxDefaultCostDetailsMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(YardInfo yardInfo, List<JzxDefaultCostDetails> jzxDefaultCostDetailsList) {
        yardInfoMapper.updateById(yardInfo);

        //1.先删除子表数据
        jzxDefaultCostDetailsMapper.deleteByMainId(yardInfo.getId());

        //2.子表数据重新插入
        if (jzxDefaultCostDetailsList != null && jzxDefaultCostDetailsList.size() > 0) {
            for (JzxDefaultCostDetails entity : jzxDefaultCostDetailsList) {
                //外键设置
                entity.setCostId(yardInfo.getId());
                jzxDefaultCostDetailsMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        jzxDefaultCostDetailsMapper.deleteByMainId(id);
        yardInfoMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            jzxDefaultCostDetailsMapper.deleteByMainId(id.toString());
            yardInfoMapper.deleteById(id);
        }
    }

}
